package samples.examples.big_ben.views;


import divtastic.js.DisplayDiv;
import divtastic.js.Drawing;
import divtastic.js.application.views.WindowView;


using divtastic.js.Drawing;
using samples.examples.big_ben.views.BigBenView;
class BigBenView
{
    
    
    
    private var _holder:        DisplayDiv;
    private var _win:           WindowView;
    
    //These need to be Divtastic's to allow rotation
    private var _hourHand:      DisplayDiv;
    private var _minHand:       DisplayDiv;
    private var _secHand:       DisplayDiv;
    //private var _sec:           Int;
    private var _min:           Int;
    private var _hour:          Int;
    private var origTitle:      String;
    
    
    public function new( holder_: DisplayDiv, win_: WindowView )
    {
        
        _holder     = holder_;
        _win        = win_;
        var dGap    = 10;
        var hGap    = Std.int( WindowView.headerHeight + dGap );
        var w       = Std.int( _holder.width - dGap*2 );
        var h       = Std.int( _holder.height - hGap - dGap );
        /*
        _holder.drawGradElipse  (   Std.int( dGap )
                                ,   hGap 
                                ,   w
                                ,   h
                                ,   0xebe9eb
                                ,   0xf7e9f2
                                ,   1
                                ,   'vertical'
                                );
        */
        var clockFace = new DisplayDiv( 'assets/bigben.png' );
        clockFace.x = dGap;
        clockFace.y = hGap;
        clockFace.width = 517;
        clockFace.height = 517;
        _holder.addChild( clockFace );
        
        var cx      = dGap + w/2;
        var cy      = hGap + h/2;
        origTitle   = _win.title;
        _hourHand   = new DisplayDiv();
        _hourHand.x = cx - 38/2;
        _hourHand.y = cy - 92/2;
        _hourHand.drawHand( 38, 164, 92, 'assets/shortHand.png' );
        
        _holder.addChild( _hourHand );
        
        _minHand    = new DisplayDiv();
        _minHand.x  = cx - 35/2;
        _minHand.y  = cy - 98/2;
        _minHand.drawHand( 35, 296, 98, 'assets/bigHand.png' );
        _holder.addChild( _minHand );
        
        
        var timer = new haxe.Timer(1000);
        timer.run = updateRotation;
        updateRotation();
        
    }
    
    
    public function updateRotation()
    {
        
        var currTime    = Date.now();
        var min         = currTime.getMinutes();
        var hour        = currTime.getHours();
        //_sec            = currTime.getSeconds();
        if( hour != _hour )
        {
            
            _hourHand.rotation = (360/12)*hour%12 ;
            
        }
        
        if( min != _min )
        {
            
            _minHand.rotation = (360/60)*min ;
            
        }
        
        //testHand.rotation   = 2*(360/60)*_sec ;
        _min                = min;
        _hour               = hour;
        _win.title = origTitle + '  - ' + _hour + ':' + _min ;
        
    }
    
    
    //use
    private static function drawHand( scope: DisplayDiv, width: Int, length: Int, dy: Int, img: String  )
    {
        
        var inside      = new DisplayDiv( img );
        inside.x        = 0;
        inside.y        = -length + dy;
        inside.width    = width;
        inside.height   = length;
        scope.addChild( inside );
        
    }
    
    
}